﻿Imports System.Text
Imports IICBusinessLogic.PeriodProcessing
Imports org.model.lib
Imports IICBusinessLogic
Imports org.model.lib.db

<TestClass()>
Public Class Views
    <TestMethod()>
    Public Sub testSQLViewsSyntax()

        Dim dt As DataTable = DBUtils.Current.getDataTable("SELECT name FROM sys.views")

        For Each dr As DataRow In dt.Rows

            Dim sql As String = String.Format("select count(*) from {0}", dr("name"))
            DBUtils.Current.executeSQL(sql)
            Trace.TraceInformation("{0}: ok", dr("name"))

        Next

    End Sub

End Class

<TestClass()>
Public Class Periods

    Public Sub TestProcessFisrtDASPeriod()

        Try
            Model.ModelContext.CurrentUser = New Model.ModelObjectPrincipal("test")

            Model.ModelContext.beginTrans()
            Dim p As Period = PeriodDataUtils.findOne("periodid=78")
            Dim pp As New PeriodProcessor(p)
            pp.processPeriodAndStoreToDb()

            For Each cc As CertificationEarned In pp.certificatesEarned

                Dim cct As CertificationType = CertificationTypeDataUtils.findByKey(cc.CertificationTypeId.GetValueOrDefault)
                Assert.IsTrue( _
                    cct.DiplomaSuperTypeId.GetValueOrDefault = EnumDiplomaSuperTypes.DAS)

            Next


            For Each dd As DiplomaEarned In pp.diplomasEarned

                Dim dplma As DiplomaType = DiplomaTypeDataUtils.findByKey(dd.DiplomaTypeId.GetValueOrDefault)
                Assert.IsTrue( _
                    dplma.DiplomaSuperTypeId.GetValueOrDefault = EnumDiplomaSuperTypes.DAS)

            Next
            'do not commit here!

        Finally
            Model.ModelContext.rollbackTrans()
            Model.ModelContext.release()
        End Try

    End Sub


    <TestMethod()>
    Public Sub TestPreviousPeriod()

        Period.AllSystemPeriods = New SortedSet(Of Period)

        Dim p1 As Period = PeriodFactory.Create(DateAdd(DateInterval.Month, -1, Now))
        Dim p2 As Period = PeriodFactory.Create(DateAdd(DateInterval.Month, -2, Now))

        Period.AllSystemPeriods.Add(p1)
        Period.AllSystemPeriods.Add(p2)

        Assert.AreEqual(p2, p1.getPreviousPeriod)
        Assert.IsNull(p2.getPreviousPeriod)
        Assert.AreEqual(0, p1.distanceFromOtherPeriod(p1))
        Assert.AreEqual(1, p1.distanceFromOtherPeriod(p2))
        Assert.AreEqual(1, p2.distanceFromOtherPeriod(p1))

        Dim p3 As Period = PeriodFactory.Create(DateAdd(DateInterval.Month, -3, Now))
        Period.AllSystemPeriods.Add(p3)
        Assert.AreEqual(2, p1.distanceFromOtherPeriod(p3))
        Assert.AreEqual(2, p3.distanceFromOtherPeriod(p1))
        Assert.AreEqual(0, p3.distanceFromOtherPeriod(p3))
        Assert.AreEqual(1, p2.distanceFromOtherPeriod(p3))
        Assert.AreEqual(1, p3.distanceFromOtherPeriod(p2))

    End Sub
End Class
